/**
 * @class   wosh::security::AlarmTrigger
 * @brief   The class defines a generic alarm-system trigger.
 *          AlarmTrigger generates and append new AlarmEvents to current state.
 *
 *
 ****************************************************************************
 * @version $Id: AlarmTrigger.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Security_AlarmTrigger_H__
 #define __WOSH_Security_AlarmTrigger_H__

 #include <woshDefs.h>
 #include <core/Fact.h>
 
 #include <framework/security/AlarmEvent.h>
 #include <framework/security/AlarmState.h>

 #include <framework/building/Room.h>


namespace wosh {
 namespace security {

class AlarmTrigger :	public Object
 {
	WOSH_SERIALIZABLE(wosh::security::AlarmTrigger)
	WOSH_COMPARE_RTTI(wosh::security::AlarmTrigger)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		AlarmTrigger() : Object() {
			this->uid = Utilities::randomLong();
			this->uid = _GET_Abs(this->uid);
			Object::setName("AlarmTrigger", this->uid );
			this->enabled = true;
			this->multLevel = 0;
			this->refLevel = 0;
		 }
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		AlarmTrigger( const AlarmTrigger& m ) : Object(m) {
			*this = m;
		 }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~AlarmTrigger() { }



	public:

		virtual long evaluateMessage( const Message* message, AlarmState* currentState )
		 {
			if ( message == NULL || message->isEmpty() || currentState == NULL ) return 0;

			// is a Building Object event?
			if ( message->getSourceBus() != _Bus_Building )
				return 0;
			// it's an event/fact ?
			if ( !message->getContent()->isFact() )
				return 0;
			const Fact* fact_event = message->getContent()->asFact();

			// is a (movement) sensor?!
			///@bug SYSTEM PROBLEM! how to detect this?!?????

			// is (right) event?
			if ( fact_event->getEvent() != _Room_EVENT_PresenceChanged )
				return 0;
			const ISerializable* obj = fact_event->getData();
			if ( obj->isKindOf<Variant>() == 0 ) {
				const wosh::Variant* obj_data = dynamic_cast<const wosh::Variant*>(obj);
				if ( obj_data->toString() != "DETECTED" ) {
					;
				 }
			 }

			return raiseEvent(currentState, this->description );
		 }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:
		long getID() const									{ return this->uid; }

		bool isEnabled() const								{ return this->enabled; }

		const std::string& getDescription() const			{ return this->description; }

		long getAlarmLevelRef()	const						{ return this->refLevel; }
		double getAlarmLevelMultiplier() const				{ return this->multLevel; }

		///< event (alarm) level, will be added(or subtracted) to global
		long getAlarmLevel() const							{ return this->multLevel * this->refLevel; }

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		void setEnabled( bool value )						{ this->enabled = value; }

		void setAlarmLevelRef( long value )					{ this->refLevel = value; }
		void setAlarmLevelMultiplier( double value )		{ this->multLevel = value; }

		void setDescription( const std::string& desc )		{ this->description = desc; }


//@}
/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:

		virtual AlarmTrigger& operator=(const AlarmTrigger& m)
		 {
			if (this == &m) return *this; // same object?

			this->uid = m.uid;
			this->enabled = m.enabled;
			this->multLevel = m.multLevel;
			this->refLevel = m.refLevel;

			return *this;
		 }

		virtual bool operator==(const AlarmTrigger &other) const
		 {
			if ( this->multLevel != other.multLevel )
				return false;

			if ( this->refLevel != other.refLevel )
				return false;

			return true;
		 }

		virtual bool operator!=(const AlarmTrigger &other) const	{ return !(*this == other);  }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const		{ return ""; }
		virtual bool updateHash( DataHashContext* hash_context ) const {
			if ( hash_context == NULL ) return false;
			bool ret = false;
			return ret;
		 }
//@}
	protected:

		virtual long raiseEvent( AlarmState* currentState, const std::string& descr )
		 {
			AlarmEvent* alarm_event = new AlarmEvent();
			alarm_event->setAlarmLevel( getAlarmLevel() );
			alarm_event->setDescription( descr );
			currentState->raiseEvent(alarm_event);
			return getAlarmLevel();
		 }

	protected:

		long uid;			///< unique id of the event
		bool enabled;		///< trigger enabled/not (used within AlarmEngine thread)

		std::string description;	///< description of the event

		double multLevel;	///< multiplier used to calculate the effective Alarm Level [0.0, 1.0]
		long refLevel;		///< reference (max) alarm level, multiplied with multLevel




}; // class def

 }; // namespace security
}; // namespace wosh

#endif //__WOSH_Security_AlarmTrigger_H__
